package com.pro.interceptor;

import java.util.Date;
import java.util.Enumeration;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.pro.beans.Alarm;
import com.pro.beans.Menu;
import com.pro.beans.User;
import com.pro.dao.UserDao;
import com.pro.entity.AlarmType;
import com.pro.entity.ResultType;
import com.pro.service.AlarmService;

public class MyInterceptor extends HandlerInterceptorAdapter {
	private String mappingURL;// 利用正则映射到需要拦截的路径
	private final static String[] ignoreUrls = { "login.do", "userinfo.do", "left.do", "top.do","enterModifyPwd.do" ,"addUser.do"};

	@Resource
	public AlarmService alarmSerivce;
	@Resource
	public UserDao userDao;

	public void setMappingURL(String mappingURL) {
		this.mappingURL = mappingURL;
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
			Exception arg3) throws Exception {
		String menuId = (String)request.getAttribute("menuId");
		if(menuId == null){
			menuId = "1";
		}
		User user = (User)request.getSession().getAttribute("user");
		Integer type = 0;
		if(user!=null){
			type = user.getType();
		}
		request.setAttribute("menus",Menu.getMenuById(menuId,type));
		String jspName = (String)request.getAttribute("jspName");
/*		if(jspName != null){
			request.setAttribute("jspName", jspName);
		}*/
		if(jspName=="alarmList" && type==1){
			jspName = "gridInfo";
			menuId = "2";
		}
		request.setAttribute("jspName", jspName);
		request.setAttribute("menuId", menuId);
		
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
		String menuId = (String)request.getParameter("menuId");
		String count = request.getParameter("page.count");
		String pageNo = request.getParameter("page.pageNo");
		String name1 = (String)request.getParameter("name");
		String password = (String)request.getParameter("password");
		User user = (User)request.getSession().getAttribute("user");
		String url = request.getRequestURL().toString();
		if(user==null && name1!=null && password!=null && !name1.equals("")&& !password.equals("")){
			User log = new User();
			log.setName(name1);
			log.setPassword(password);
			user = userDao.getObjectByObject(log);
		}
		if (user == null ) {
			if(!url.contains("login.do")&& !url.contains("logout.do")){
				request.getRequestDispatcher("/login.do").forward(request, response);
				return false;
			}
		}
		request.setAttribute("count", count);
		request.setAttribute("pageNo", pageNo);
		System.out.println(">>>>>>pageNo:"+request.getParameter("page.count"));
		System.out.println(">>>>>>pageCount:"+request.getParameter("page.pageNo"));
		if(menuId == null){
			menuId = (String)request.getAttribute("menuId");
			if(menuId == null){
				if(user==null||user.getType()==2 || user.getType()==0){
					menuId = "1";
				}else{
					menuId="2";
				}
			}
		}
		String type =(String)request.getParameter("type");
		request.setAttribute("type", type);
		if(type!=null){
			request.setAttribute("alarmType", type);
		}
		String newId =(String)request.getParameter("newId");
		String jspName =(String)request.getParameter("jspName");
		if(jspName==null){
			jspName = (String)request.getAttribute("jspName");
		}
		String tid =(String)request.getParameter("id");
		request.setAttribute("id", tid);
		Integer typeM = 0;
		if(user!=null){
			typeM = user.getType();
		}
		Menu[] menus = Menu.getMenuById(menuId,typeM);
		request.setAttribute("menus",menus);
		request.setAttribute("menuId",menuId);
		request.setAttribute("newId",newId);
		String topName = "警情管理";
		switch (menuId) {
		case "1":
			break;
		case "2":
			topName="网格管理";
			break;
		case "3":
			topName="系统设置";
			break;
		case "4":
			topName="账户管理";
			break;
		default:
			break;
		}
		
		request.setAttribute("topName", topName);
		String urlName="警情总览";
		if(jspName==null){
			if(user==null||user.getType()==2|| user.getType()==0){
				jspName = "alarmList";
			}else{
				jspName="gridInfo";
			}
		}
		if(jspName.equals("addAlarm")){
			String id = (String)request.getParameter("id");
			String dealAlarm = (String)request.getParameter("dealAlarm");
			if(id!=null&&!id.equals("")){
				Alarm alarm = alarmSerivce.getAlarmById(Integer.valueOf(id));
				request.setAttribute("alarm", alarm);
			}else if(dealAlarm!=null){
				request.setAttribute("msg", "请选中一个警情!");
				jspName = "alarmList";
			}
		}
		if(jspName.equals("modifyUser")){
			String id = (String)request.getParameter("id");
			if(id==null||id.equals("")){
				request.setAttribute("msg", "请选中一个用户!");
			}
		}
		String name = jspName;
		if(jspName.equals("alarmType")){
			name = jspName+type;
		}
		request.setAttribute("jspNameUse", name);
		for( Menu menu:menus){
			if(menu.getId().equals(name)){
				urlName = menu.getName();
			}
		}
		request.setAttribute("urlName", urlName);
		request.setAttribute("jspName", jspName);
		request.setAttribute("resultType", ResultType.values());
		request.setAttribute("alarmType", AlarmType.values());
		User user1 = (User) request.getSession().getAttribute("user");
		return true;
	}

}
